#include <stdio.h>
#include <stdlib.h> /*needed to convert a string to an integer*/

int getanInt();/*function prototype*/

int main()
{
    int value;

    printf("Enter an integer value: ");
    value=getanInt();
    printf("The integer entered is: %d\n",value);

    return 0;
  }

#define TRUE 1
#define FALSE 0
#define MAXCHARS 40
int getanInt()
{
    int isvalidInt(char []);/*function prototype*/

    int isanInt= FALSE;
    char value[MAXCHARS];

    do
    {
        gets(value);
        if(isvalidInt(value)==FALSE)
        {
            printf("Invalid integer - Please re-enter: ");
            continue;
        }
        isanInt=TRUE;
    } while (isanInt==FALSE);

    return (atoi(value));
    }

int isvalidInt(char val[])
{
    int start = 0;
    int i;
    int valid = TRUE;
    int sign = FALSE;
    /*check for an empty string*/
    if (val[0] == '\0')   valid = FALSE;

    /*check for a leading sign*/
    if (val[0] == '-' || val[0] == '+')
    {
        sign = TRUE;
        start = 1; /*start checking for digits after the sign*/
    }

    /*check that there is at least one character after the sign*/
    if (sign == TRUE && val[1] == '\0') valid = FALSE;

    /*now check the string, which we know has at least one non-sign char*/
    i = start;
    while (valid == TRUE && val[i] != '\0')
    {
        if (val[i] < '0' || val[i] > '9') /*check for a non-digit*/
            valid = FALSE;
        i++;
    }
    return valid;
}